#include "napi/native_api.h"
#include "sub/Sub.h"

static napi_value Add(napi_env env, napi_callback_info info)
{
    size_t argc = 2;
    // 声明参数数组
    napi_value args[2] = {nullptr};
    // 获取传入的参数并依次放入参数数组中
    napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);

//     napi_valuetype valuetype0;
//     napi_typeof(env, args[0], &valuetype0);
//
//     napi_valuetype valuetype1;
//     napi_typeof(env, args[1], &valuetype1);

    // 依次获取参数
    double value0;
    napi_get_value_double(env, args[0], &value0);

    double value1;
    napi_get_value_double(env, args[1], &value1);

    // 返回两数相加的结果
    napi_value sum;
    //Sub sub(value0, value1);
    Sub *sub = new Sub(value0, value1);
    napi_create_double(env, sub->testSub(), &sum);
    delete sub;
    return sum;
}

static napi_value Sub(napi_env env, napi_callback_info info) {
    size_t argc = 2;
    napi_value args[2] = {nullptr};
    
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    
    double value0;
    napi_get_value_double(env, args[0], &value0);
    double value1;
    napi_get_value_double(env, args[1], &value1);
    napi_value res;
    napi_create_double(env, value0 - value1, &res);
    return res;
}


static napi_value NativeCallArkTS(napi_env env, napi_callback_info info) {
    size_t argc = 1;
    // 声明参数数组
    napi_value args[1] = {nullptr};

    // 获取传入的参数并依次放入参数数组中
    napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);

    // 创建一个int，作为ArkTS的入参
    napi_value argv0 = nullptr;    
    napi_create_int32(env, 2, &argv0 );
    napi_value argv1 = nullptr;    
    napi_create_int32(env, 30, &argv1 );

    napi_value argv[2] = {argv0, argv1};
    
    // 调用传入的callback，并将其结果返回
    napi_value result = nullptr;
    napi_call_function(env, nullptr, args[0], 2, argv, &result);
    
    int value0;
    napi_get_value_int32(env, result, &value0);
    napi_create_int32(env, value0 * 2, &result);
    return result;
}

struct CallbackData {
    napi_async_work asyncWork = nullptr;
    napi_deferred deferred = nullptr;
    napi_ref callback = nullptr;
    double args = 0;
    double result = 0;
};


static void ExecuteCB(napi_env env, void *data)
{
    CallbackData *callbackData = reinterpret_cast<CallbackData *>(data);
    callbackData->result = callbackData->args;
}

static void CompleteCB(napi_env env, napi_status status, void *data)
{
    CallbackData *callbackData = reinterpret_cast<CallbackData *>(data);
    napi_value result = nullptr;
    napi_create_double(env, callbackData->result, &result);
    if (callbackData->result > 0) {
        napi_resolve_deferred(env, callbackData->deferred, result);
    } else {
        napi_reject_deferred(env, callbackData->deferred, result);
    }

    napi_delete_async_work(env, callbackData->asyncWork);
    delete callbackData;
}

static napi_value AsyncWork(napi_env env, napi_callback_info info)
{
   size_t argc = 1;
   napi_value args[1];
   napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);

   napi_value promise = nullptr;
   napi_deferred deferred = nullptr;
   napi_create_promise(env, &deferred, &promise);

   auto callbackData = new CallbackData();
   callbackData->deferred = deferred;
   napi_get_value_double(env, args[0], &callbackData->args);

   napi_value resourceName = nullptr;
   napi_create_string_utf8(env, "AsyncCallback", NAPI_AUTO_LENGTH, &resourceName);
   // 创建异步任务
   napi_create_async_work(env, nullptr, resourceName, ExecuteCB, CompleteCB, callbackData, &callbackData->asyncWork);
   // 将异步任务加入队列
   napi_queue_async_work(env, callbackData->asyncWork);

   return promise;
}

static napi_value IndexOutOfBounds(napi_env env, napi_callback_info info) {
    // 触发下标越界异常
    int a[10];
    // 构造数组越界写入
    a[10] = 1;
}

static napi_value NullPointerException(napi_env env, napi_callback_info info) {
    class Sub *sub = nullptr;
    sub->testSub();
}


EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports) {
    napi_property_descriptor desc[] = {
        {"add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"sub", nullptr, Sub, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"indexOutOfBounds", nullptr, IndexOutOfBounds, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"nullPointerException", nullptr, NullPointerException, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"nativeCallArkTS", nullptr, NativeCallArkTS, nullptr, nullptr, nullptr, napi_default, nullptr}};
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END

static napi_module demoModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "entry",
    .nm_priv = ((void*)0),
    .reserved = { 0 },
};

extern "C" __attribute__((constructor)) void RegisterModule(void)
{
    napi_module_register(&demoModule);
}
